<template>
  <div>
    <el-form labelWidth="150px" :disabled="status==1">
      <el-form-item label="故障现象与维修要求" class="is-required" style="width:100%">
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="repairData.content"
          clearable
        ></el-input>
        <div class="el-form-item__error" v-if="!$v.repairData.content.required">请输入故障现象与维修要求</div>
      </el-form-item>
      <el-form-item label="设备" class="is-required" style="width:100%">
        <selecteq
          :selecteqid.sync="selecteqid"
          style="width:90%;margin-right:10px;display:inline-block"
        ></selecteq>
        <el-button
          size="mini"
          type="primary"
          @click="addshebei"
          style="float:right;margin-right:5px;margin-top:5px"
        >添加</el-button>

        <div class="el-form-item__error" v-if="!$v.repairData.shop.required">请输入设备名称</div>
      </el-form-item>
      <el-form-item label="已选设备" class="is-required" style="width:100%">
        <div v-for="(item, index) in shebei" :key="index">
          <span>{{item.value}}</span>
          <i
            class="el-icon-delete"
            style="font-size:16px;float:right"
            @click="deleteconfirmdely(index)"
          ></i>
        </div>
      </el-form-item>
      <el-form-item label="申请时间" class="is-required" style="width:100%">
        <el-date-picker
          v-model="repairData.examinetime"
          type="date"
          style="width:100%;"
          placeholder="选择申请时间"
          valueFormat="yyyy-MM-dd"
          clearable
        ></el-date-picker>
        <div class="el-form-item__error" v-if="!$v.repairData.examinetime.required">请选择申请时间</div>
      </el-form-item>
      <el-form-item label="处理方式" class="is-required" style="width:100%">
        <el-select v-model="repairData.detype" placeholder="请选择" style="width:100%;">
          <el-option
            v-for="item in itemdata"
            :key="item.hash.code_name"
            :label="item.hash.code_name"
            :value="item.hash.code_id"
          ></el-option>
        </el-select>
        <div class="el-form-item__error" v-if="!$v.repairData.examinetime.required">请选择处理方式</div>
      </el-form-item>
      <el-form-item label="申请金额(元)" class="is-required" style="width:100%">
        <el-input v-model="repairData.amount" clearable></el-input>
        <div class="el-form-item__error" v-if="!$v.repairData.amount.required">请输入申请金额</div>
        <div class="el-form-item__error" v-if="!$v.repairData.amount.money">金额格式有误</div>
      </el-form-item>
      <el-form-item label="大写金额" style="width:100%">{{this.convertCurrency(repairData.amount)}}</el-form-item>
      <el-form-item label="服务商家" class="is-required" style="width:100%">
        <el-input v-model="repairData.shop" clearable></el-input>
        <div class="el-form-item__error" v-if="!$v.repairData.shop.required">请输入服务商家</div>
      </el-form-item>
      <el-form-item label="维修备注" style="width:100%">
        <el-input v-model="repairData.beizhu" clearable></el-input>
      </el-form-item>
      <el-form-item label="维修附件" style="width:100%">
        <upload2 :filelimit="5" :filelink.sync="filearr1" :ispic="false" :isedit="false"></upload2>
      </el-form-item>
      <el-form-item label="选择审批流" v-if="status!=1" class="is-required" style="width:100%">
        <choosespl @getsplid="getsplid" :atypeid="'0106'"></choosespl>
        <div class="el-form-item__error" v-if="!$v.aid.required">请选择审批流</div>
      </el-form-item>
    </el-form>

    <div style="text-align:right" v-if="status==0">
      <el-button size="mini" @click="closedia">取 消</el-button>
      <el-button size="mini" type="primary" @click="submitrepair">确 定</el-button>
    </div>
    <div style="text-align:right" v-if="status==2">
      <el-button size="mini" @click="closedia">取 消</el-button>
      <el-button size="mini" type="primary" @click="submitrepair2">重新提交</el-button>
    </div>
  </div>
</template>

<script>
import choosespl from '~/components/apply/choosespl.vue';
import upload2 from '~/components/common/upload2.vue';
import { required } from 'vuelidate/lib/validators';
import { regex } from 'vuelidate/lib/validators/common.js';
import selecteq from '~/components/common/selecteq.vue';

const money = regex('money', /^\d{1,8}(\.\d{1,2})?$/);

export default {
  validations: {
    aid: {
      required,
    },
    repairData: {
      content: {
        required,
      },
      examinetime: {
        required,
      },
      amount: {
        required,
        money,
      },
      shop: {
        required,
      },
    },
  },
  data() {
    return {
      flag: false,
      dialogVisible: true,
      ts: this.newStore(),
      filearr1: [],
      repairData: {},
      aid: '',
      selecteqid: {},
      shebei: [],
      shebeiid: [],
      itemdata: [],
      detype: '',
    };
  },
  components: {
    choosespl,
    upload2,
    selecteq,
  },
  props: {
    isnew: Boolean,
    status: Number,
    checkdetail: Object,
    fun: Function,
  },
  created() {
    if (this.status != 0) {
      for (let i = 0; i < this.checkdetail.operation2.length; i += 1) {
        const a = {};
        a.url = this.checkdetail.operation2[i].hash.fileurl;
        a.name = this.checkdetail.operation2[i].hash.filename;
        this.filearr1.push(a);
      }
      for (let i = 0; i < this.checkdetail.operation3.length; i += 1) {
        this.shebei[i] = this.checkdetail.operation3[i].hash;

        this.shebei[i].value = this.checkdetail.operation3[i].hash.managenum;
      }
      this.repairData = this.checkdetail.operation1[0].hash;
    }

    this.getshebei();
  },
  methods: {
    checksplnum(money2, splid) {
      const req = {
        handle:
          '9000B8B8D347526960C5BAB1FB6894EF3277431D02D636A5015C1425899A1F59A41CF7A197311E4A',
        flowid: splid,
        number: money2,
      };
      const p = this.spost(this.ts, '/mainservlet', req).then((ts) => {
        if (ts.d.data.operation1[0].hash.cou == '0') {
          this.flag = false;
        } else {
          this.flag = true;
        }
      });
      return p;
    },
    addshebei() {
      this.shebei.push(this.selecteqid);
    },
    deleteconfirmdely(index) {
      if (this.status == 0 || this.status == 2) {
        this.shebei.splice(index, 1);
      }
    },
    // 获取处理方式
    getshebei() {
      // this.eqList1 = [];
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B7D59D38996D29503FD58B6E268D37EF8',
        codetype: '04',
        supid: '0404',
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        // console.log(ts.d.data, "00000000");
        this.itemdata = ts.d.data.operation1;
      });
    },
    closedia() {
      this.$emit('closedia', false);
      this.$router.go(-1);
    },
    // 重新提交
    submitrepair2() {
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else {
        this.checksplnum(this.repairData.amount, this.aid).then(() => {
          if (this.flag == false) {
            this.$message({
              type: 'error',
              message: ' 审批流不符合条件，请重新选择',
            });
            return;
          }
          this.$confirm('确认重新提交设备维修审批?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              const req = {
                handle:
                  '9000B8B8D347526960C5BAB1FB6894EF17427C100F77D58DB382894F762E66D209652941AB86E5AD',
                atype: '0106',
                flowid: this.aid,
                approvalid: this.repairData.aid,
              };
              this.spost(this.ts, '/mainservlet', req).then((ts) => {
                const req2 = this.repairData;
                req2.aid = ts.d.data.operation3[0].hash.approvalid;
                req2.handle =
                  '9DEA590540016AF56AA8E4B54801438BB3F2193D08E31332D5E5601D2F82C2E6F597A4049D90C42F64EDEA66339D7026';
                req2.bigamount = this.convertCurrency(this.repairData.amount);
                req2.fileurl = this.posturl(this.filearr1);
                req2.filename = this.postname(this.filearr1);
                console.log(this.shebei, '000');

                for (let m = 0; m < this.shebei.length; m++) {
                  this.shebeiid.push(this.shebei[m].cccid);
                  this.shebeiid.push(this.shebei[m].id);
                }
                req2.cccid = this.shebeiid;
                this.spost(this.ts, '/mainservlet', req2).then(() => {
                  this.$message({
                    type: 'success',
                    message: '重新提交设备维修审批成功！',
                  });
                  this.fun();
                });
              });
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消',
              });
            });
        });
      }
    },
    submitrepair() {
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else {
        this.checksplnum(this.repairData.amount, this.aid).then(() => {
          if (this.flag == false) {
            this.$message({
              type: 'error',
              message: ' 审批流不符合条件，请重新选择',
            });
            return;
          }
          this.$confirm('确认提交设备维修审批?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              const req = {
                handle:
                  '9000B8B8D347526960C5BAB1FB6894EF8B7B312373E9BEF47A8517CE4098C19A',
                atype: '0106',
                flowid: this.aid,
              };
              this.spost(this.ts, '/mainservlet', req).then((ts) => {
                const req2 = this.repairData;
                req2.aid = ts.d.data.operation3[0].hash.approvalid;
                req2.handle =
                  '9DEA590540016AF56AA8E4B54801438BB3F2193D08E31332D5E5601D2F82C2E61C2EAA69CA7B1203';

                req2.bigamount = this.convertCurrency(this.repairData.amount);
                req2.detype = this.repairData.detype;
                req2.fileurl = this.posturl(this.filearr1);
                req2.filename = this.postname(this.filearr1);
                for (let m = 0; m < this.shebei.length; m++) {
                  this.shebeiid.push(this.shebei[m].id);
                }
                req2.cccid = this.shebeiid;
                this.spost(this.ts, '/mainservlet', req2).then(() => {
                  this.$message({
                    type: 'success',
                    message: '发起设备维修审批成功！',
                  });
                  this.$emit('closedia', false);
                });
              });
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消',
              });
            });
        });
      }
    },
    // 公共方法图片数组转路径数组
    posturl(arr) {
      const a = [];
      for (let i = 0; i < arr.length; i += 1) {
        a.push(arr[i].url);
      }
      return a;
    },
    // 公共方法图片数组转名称数组
    postname(arr) {
      const b = [];
      for (let i = 0; i < arr.length; i += 1) {
        b.push(arr[i].name);
      }
      return b;
    },
    getsplid(val) {
      this.aid = val;
    },
    // 金额转大写
    convertCurrency(money) {
      // 汉字的数字
      const cnNums = new Array(
        '零',
        '壹',
        '贰',
        '叁',
        '肆',
        '伍',
        '陆',
        '柒',
        '捌',
        '玖',
      );
      // 基本单位
      const cnIntRadice = new Array('', '拾', '佰', '仟');
      // 对应整数部分扩展单位
      const cnIntUnits = new Array('', '万', '亿', '兆');
      // 对应小数部分单位
      const cnDecUnits = new Array('角', '分', '毫', '厘');
      // 整数金额时后面跟的字符
      const cnInteger = '整';
      // 整型完以后的单位
      const cnIntLast = '元';
      // 最大处理的数字
      const maxNum = 999999999999999.9999;
      // 金额整数部分
      let integerNum;
      // 金额小数部分
      let decimalNum;
      // 输出的中文金额字符串
      let chineseStr = '';
      // 分离金额后用的数组，预定义
      let parts;
      if (money == '') {
        return '';
      }
      money = parseFloat(money);
      if (money >= maxNum) {
        // 超出最大处理数字
        return '';
      }
      if (money == 0) {
        chineseStr = cnNums[0] + cnIntLast + cnInteger;
        return chineseStr;
      }
      // 转换为字符串
      money = money.toString();
      if (money.indexOf('.') == -1) {
        integerNum = money;
        decimalNum = '';
      } else {
        parts = money.split('.');
        integerNum = parts[0];
        decimalNum = parts[1].substr(0, 4);
      }
      // 获取整型部分转换
      if (parseInt(integerNum, 10) > 0) {
        let zeroCount = 0;
        const IntLen = integerNum.length;
        for (var i = 0; i < IntLen; i++) {
          var n = integerNum.substr(i, 1);
          const p = IntLen - i - 1;
          const q = p / 4;
          const m = p % 4;
          if (n == '0') {
            zeroCount++;
          } else {
            if (zeroCount > 0) {
              chineseStr += cnNums[0];
            }
            // 归零
            zeroCount = 0;
            chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
          }
          if (m == 0 && zeroCount < 4) {
            chineseStr += cnIntUnits[q];
          }
        }
        chineseStr += cnIntLast;
      }
      // 小数部分
      if (decimalNum != '') {
        const decLen = decimalNum.length;
        for (var i = 0; i < decLen; i++) {
          var n = decimalNum.substr(i, 1);
          if (n != '0') {
            chineseStr += cnNums[Number(n)] + cnDecUnits[i];
          }
        }
      }
      if (chineseStr == '') {
        chineseStr += cnNums[0] + cnIntLast + cnInteger;
      } else if (decimalNum == '') {
        chineseStr += cnInteger;
      }
      return chineseStr;
    },
  },
};
</script>
